<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends CommonController {
	//商品
    public function goodslist()
    {
        $goods = M('goods'); // 实例化User对象
        $count      = $goods->where('is_delete=1')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出

        $sql = 'select t1.*,t2.parent_id,t2.cat_name from goods as t1 join cat as t2 on t1.cat_id = t2.cat_id where t1.is_delete=1 limit '. $Page->firstRow.','.$Page->listRows;
        $goods = M()->query($sql);
        $this->assign('goods',$goods);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        
        $this->display();
    }

    //商品添加 
    public function goodsadd()
    {
        if (IS_POST) {
            $data['goods_name'] = I('post.goods_name');
            $data['cat_id'] = I('post.cat_id');
            $data['shop_price'] = I('post.shop_price');
            $data['goods_number'] = I('post.goods_number');
            $data['goods_desc'] = I('post.goods_desc');

            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->savePath  =      'Uploads/'; // 设置附件上传目录    
            // 上传文件     
            $info   =   $upload->upload();    
            if(!$info) {// 上传错误提示错误信息         
                $this->error($upload->getError());
            }   
            // 上传成功   
            $info = current($info);//减维 
            //入库原图路径
            $ori_url =  '/'.'Uploads/'.$info['savepath'].$info['savename'];    

            //根据原图生成缩略图
            $image = new \Think\Image(); 
            $image->open('.'.$ori_url);
            //生成缩略图路径
            $thumb_path = './Uploads/thumb/'.$info['savename'];
            // 按照原图的比例生成一个最大为250*250的缩略图并保存为$thumb_path
            $image->thumb(250, 350)->save($thumb_path);
            //入库缩略图路径
            $thumb_url = '/Uploads/thumb/'.$info['savename'];

            $data['ori_img'] = $ori_url;
            $data['thumb_img'] = $thumb_url;

            $data['add_time'] = time();
            //入库
            $res = M('goods')->add($data);
            if ($res) {
                // $url = U('admin/goods/goodslist');
                $this->redirect('admin/goods/goodslist');
            }

        }else {
            //取出所有分类
            $cats = M('cat')->select();
            $cats = gettree($cats);
            $this->assign('cats',$cats);
            
            $this->display();
        }
    }

    //商品添加显示 (异步添加 未用)
    // public function goods_add()
    // {//Array ( [goods_name] => x [cat_id] => 1 [shop_price] => x [goods_number] => 1 [goods_img] => [new_goods_img] => /Uploads/Uploads/2017-05-20/59201667a2e42.png [goods_desc] => x )
    // 	if (IS_POST) {
    //         $data['goods_name'] = I('post.goods_name');
    //         $data['cat_id'] = I('post.cat_id');
    //         $data['shop_price'] = I('post.shop_price');
    //         $data['goods_number'] = I('post.goods_number');
    //         $data['goods_desc'] = I('post.goods_desc');

    //         $data['ori_img'] = I('post.new_goods_img');
    //         $data['thumb_img'] = I('post.new_thumb_img');

    //         $data['add_time'] = time();
    //         //入库
    //         $res = M('goods')->add($data);
    //         if ($res) {
    //             $url = U('admin/goods/goodslist');
    //             $arr = array(
    //                 'error' => 1,
    //                 'info' => '商品添加成功',
    //                 'url' => $url
    //                 );
    //             echo json_encode($arr);
    //         }else {
    //             $arr = array(
    //                 'error' => 0,
    //                 'info' => '商品添加失败'
    //                 );
    //             echo json_encode($arr);
    //         }

    // 	}else {
	   //      //取出所有分类
	   //      $cats = M('cat')->select();
	   //      $cats = gettree($cats);
	   //      $this->assign('cats',$cats);
	        
	   //  	$this->display();
    // 	}

    // }

    //商品图片上传  返回文件入库相对路径
    public function good_img()
    {
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
 
        }else{// 上传成功   
            $info = current($info);//减维 
            //上传成功 原图路径
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];
            //入库原图路径
            $ori_url =  '/'.'Uploads/'.$info['savepath'].$info['savename'];    

            //根据原图生成缩略图
            $image = new \Think\Image(); 
            $image->open('.'.$ori_url);
            //生成缩略图路径
            $thumb_path = './Uploads/thumb/'.$info['savename'];
            // 按照原图的比例生成一个最大为250*250的缩略图并保存为$thumb_path
            $image->thumb(250, 350)->save($thumb_path);
            //入库缩略图路径
            $thumb_url = '/Uploads/thumb/'.$info['savename'];

            $arr = array(
            'code' => 1,
            'msg' => '图片上传成功',
            'data'=> array(
                'src' => $url,
                'ori_url' =>$ori_url,
                'thumb_url' =>$thumb_url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }

    //商品编辑
    public function goodsdit()
    {
        if (IS_POST) {
            $goods_id = I('post.goods_id');

            $data['goods_name'] = I('post.goods_name');
            $data['cat_id'] = I('post.cat_id');
            $data['shop_price'] = I('post.shop_price');
            $data['goods_number'] = I('post.goods_number');
            $data['goods_desc'] = I('post.goods_desc');

            $ori_img = I('post.new_goods_img');

            $data['update_time'] = time();

            //有图片更新删除旧文件
            if (!empty($ori_img)) {
                $data['ori_img'] = I('post.new_goods_img');
                $data['thumb_img'] = I('post.new_thumb_img');

                $del_img = M('goods')->field('ori_img,thumb_img')->where(array('goods_id'=>$goods_id))->find();
                unlink('./'.$del_img['ori_img']);
                unlink('./'.$del_img['thumb_img']);
            }

            //更新入库
            $res = M('goods')->where(array('goods_id'=>$goods_id))->save($data);
            if ($res) {
                $url = U('admin/goods/goodslist');
                $arr = array(
                    'error' => 1,
                    'info' => '商品更新成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '商品更新失败'
                    );
                echo json_encode($arr);
            }

 
        }else {
            $goods_id = I('get.goods_id');
            $goods_info = M('goods')->where(array('goods_id'=>$goods_id))->find();
            $this->assign('goods_info',$goods_info);

            //取出所有分类
            $cats = M('cat')->select();
            $cats = gettree($cats);
            $this->assign('cats',$cats);
            $this->display();
        }

    }

    //商品状态设置
    public function goodstart()
    {
        if (IS_POST) {
            $goods_id = I('post.goods_id');

            $data['is_on_sale'] = I('post.is_on_sale');
            $data['is_new'] = I('post.is_new');
            $data['is_hot'] = I('post.is_hot');

            //状态 更新
            $res = M('goods')->where(array('goods_id'=>$goods_id))->save($data);
            if ($res) {
                $url = U('admin/goods/goodslist');
                $arr = array(
                    'error' => 1,
                    'info' => '状态更新成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '状态更新失败'
                    );
                echo json_encode($arr);
            }

        }else {

            $goods_id = I('get.goods_id');
            $goods_start = M('goods')->field('goods_id,is_new,is_hot,is_on_sale')->where(array('goods_id'=>$goods_id))->find();
            $this->assign('goods_start',$goods_start);

            $this->display();
        }
    }

    //回收站 is_delete=0 
    public function goodsdel()
    {
        if (IS_POST) {
            $goods_id = I('post.goods_id');

            $res = M('goods')->where(array('goods_id'=>$goods_id))->setField('is_delete',0);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '商品放入回收站'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '商品放入回收站失败'
                    );
                echo json_encode($arr);
            }
        }
    }

    //批量放入回收站 
    public function goodsdels()
    {
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('goods')->where($w)->setField('is_delete',0);
          if ($res) {
              $arr = array(
                  'error' =>1,
                  'info'=>"商品放入回收站"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"商品放入回收站失败"
                  );
              echo json_encode($arr);
          }
      }
    }

    //回收站列表显示
    public function delslist()
    {
        $goods = M('goods'); // 实例化User对象
        $count      = $goods->where('is_delete=0')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,4);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出

        $sql = 'select t1.*,t2.cat_name from goods as t1 join cat as t2 on t1.cat_id = t2.cat_id where t1.is_delete=0 limit '. $Page->firstRow.','.$Page->listRows;
        $goods = M()->query($sql);
        $this->assign('goods',$goods);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->assign('goods',$goods);

        $this->display();
    }

    //回收站 删除/同时删除图片
    public function delete()
    {
        if (IS_POST) {
            $goods_id = I('post.goods_id');

            //查出要删除的图片路径
            $del_img = M('goods')->field('ori_img,thumb_img')->where(array('goods_id'=>$goods_id))->find();

            $res = M('goods')->delete($goods_id);
            if ($res) {
                //删除图片文件
                unlink('./'.$del_img['ori_img']);
                unlink('./'.$del_img['thumb_img']);

                $arr = array(
                    'error' => 1,
                    'info' => '商品删除成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '商品删除失败'
                    );
                echo json_encode($arr);
            }
        }
    }

    //回收站 恢复
    public function res()
    {
        if (IS_POST) {
            $goods_id = I('post.goods_id');

            $res = M('goods')->where(array('goods_id'=>$goods_id))->setField('is_delete',1);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '商品恢复成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '商品恢复失败'
                    );
                echo json_encode($arr);
            }
        }
    }



}